$(function () {
    let ws;
    if("WebSocket" in window) {
        // 获取招聘者id
        let clientID = $("#eid").val();
        // 创建WebSocket对象,需要注意请求路径
        ws = new WebSocket("ws://127.0.0.1:8081/hire/web/" + clientID);
        // 接收到服务端消息时触发
        ws.onmessage = function (evt) {
            let received_msg = evt.data;
            layer.msg(received_msg,{
                offset: '50px'
            });
        };
    }
});

Date.prototype.Format = function (fmt) {
    let o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};


layui.use(['table', 'layer'], function () {
    let table = layui.table;
    let layer = layui.layer;
    table.render({
        elem: '#post',
        title: '职位管理',
        url: '/hire/employer/recruitment/post/page',
        response: {
            statusName: 'code',
            statusCode: 200,
            msgName: 'msg',
            count: 'count',
            dataName: 'data'
        },
        height: "625px",
        toolbar: '#toolbarDemo',
        page: true,
        limit: 10,
        limits: [2, 5, 10, 15],
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {title: '序号', templet: '#xuhao', width: 70, align: 'center'},
            {field: 'id', hide: true, title: 'id', align: 'center'},
            {field: 'name', title: '名称', align: 'center'},
            {field: 'contactName', title: '联系人', align: 'center', width: 80},
            {field: 'contactPhone', title: '联系电话', align: 'center'},
            {field: 'releaseTime', title: '发布时间', width: 200, sort: true, align: 'center'},
            {field: 'frequency', title: '浏览次数', sort: true, align: 'center', width: 110},
            {field: 'relationName', toolbar: '#barDemo', title: '操作', width: 450, align: 'center'}
        ]],
        text: {none: '您没有发布职位'}
    });

    table.on('toolbar(post)', function (obj) {
        let checkStatus = table.checkStatus('post');
        let data = checkStatus.data;
        if (obj.event == 'add') {
            window.location.href = "/hire/employer/create";
        } else if (obj.event == 'delete') {
            // console.log(data);
            if (data.length == 0) {
                layer.msg('请选择至少一行进行编辑');
            } else {
                layer.confirm('请确认是否取删除职位', {
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    let ids = '';
                    for (let i = 0; i < data.length; i++) {
                        if (i == data.length - 1) {
                            ids = ids + data[i].id;
                        } else {
                            ids = ids + data[i].id + ",";
                        }
                    }
                    $.post('/hire/employer/del/recruitment/batch', {ids: ids}, function (res) {
                        if (res.code == '200') {
                            layer.msg("成功删除" + res.data + "个职位", {icon: 1});
                            table.reload('post', {
                                url: '/hire/employer/recruitment/post/page'
                            });
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                });

            }
        }
    });
    table.on('tool(post)', function (obj) {
        // console.log(obj);
        let id = obj.data.id;
        if (obj.event == 'view') {
            // console.log(obj);
            window.location.href = "/hire/employer/view?id=" + id;
        } else if (obj.event == 'update') {
            window.location.href = "/hire/employer/edit?id=" + id;
        } else if (obj.event == 'delete') {
            layer.confirm('请确认是否取删除职位', {
                btn: ['确认', '取消'] //按钮
            }, function () {
                let param = {id: id};
                $.post('/hire/employer/del/recruitment', param, function (res) {
                    if (res.code == '200') {
                        // 删除成功则跳转页面
                        layer.msg('删除成功', {icon: 1});
                        obj.del();
                        // table.reload('post', {
                        //     url: '/hire/employer/recruitment/post/page'
                        // });
                    } else {
                        layer.msg(res.msg);
                    }
                });

            });
        } else if (obj.event == 'refresh') {
            let param = {id: id};
            $.post('/hire/employer/recruitment/refresh', {id: id}, function (res) {
                console.log(res);
                if (res.code == '200') {
                    obj.update({
                        releaseTime: new Date().Format("yyyy-MM-dd HH:mm:ss")
                    });
                    // table.reload('post', {
                    //     url: '/hire/employer/recruitment/post/page'
                    // });
                    layer.msg("刷新成功", {icon: 1})
                } else {
                    layer.msg("刷新失败", {icon: 2})
                }
            });
        } else if (obj.event == 'view_resume') {
            window.location.href = "/hire/employer/recruitment/resume?id=" + id;
        }
    });
});

